package com.example.app1

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathDashPathEffect
import android.graphics.PathMeasure
import android.util.AttributeSet
import android.view.View
import java.text.SimpleDateFormat
import java.time.Clock
import java.util.Calendar
import java.util.Locale
import java.util.TimeZone
import kotlin.math.cos
import kotlin.math.min
import kotlin.math.sin

class ClockView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {

    private val radius = 150.dp
    private val smallRadius = 3.dp
    private val bigRadius = 6.dp
    private val hourHand = 70.dp
    private val minuteHand = 100.dp
    private val secondHand = 120.dp

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val circlePath = Path()
    private val smallPointPath = Path()
    private val bigPointPath = Path()
    private lateinit var smallPointEffect: PathDashPathEffect
    private lateinit var bigPointEffect: PathDashPathEffect
    private val runnable = Runnable {
        invalidate()
    }

    init {
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Paint.Cap.ROUND
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
//        circlePath.addCircle(width / 2f, height / 2f, radius, Path.Direction.CCW)
        circlePath.addArc(
            width / 2f - radius,
            height / 2f - radius,
            width / 2f + radius,
            height / 2f + radius,
            0f,
            360f
        )
        val pathMeasure = PathMeasure(circlePath, false)

//        smallPointPath.addRect(0f, 0f, smallRadius, smallRadius, Path.Direction.CCW)
        smallPointPath.addCircle(0f, 0f, smallRadius, Path.Direction.CCW)
        smallPointEffect = PathDashPathEffect(
            smallPointPath,
            pathMeasure.length / 60,
            0f,
            PathDashPathEffect.Style.ROTATE
        )

        bigPointPath.addCircle(0f, 0f, bigRadius, Path.Direction.CCW)
        bigPointEffect = PathDashPathEffect(
            bigPointPath,
            pathMeasure.length / 12,
            0f,
            PathDashPathEffect.Style.ROTATE
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        paint.strokeWidth = 2.dp
        // 画一个圆
        canvas.drawPath(circlePath, paint)
        // 画小刻度点
        paint.pathEffect = smallPointEffect
        canvas.drawPath(circlePath, paint)
        // 画大刻度点
        paint.pathEffect = bigPointEffect
        canvas.drawPath(circlePath, paint)
        paint.pathEffect = null
        // 三角函数https://zhuanlan.zhihu.com/p/390928056
        // 指针画出来
        val timeAngle = getTimeAngle()
        println(timeAngle)
        paint.strokeWidth = 6.dp
        canvas.drawLine(
            width / 2f,
            height / 2f,
            width / 2f + (cos(Math.toRadians(timeAngle.first)) * hourHand).toFloat(),
            height / 2f + (sin(Math.toRadians(timeAngle.first)) * hourHand).toFloat(),
            paint
        )
        paint.strokeWidth = 4.dp
        canvas.drawLine(
            width / 2f,
            height / 2f,
            width / 2f + (cos(Math.toRadians(timeAngle.second)) * minuteHand).toFloat(),
            height / 2f + (sin(Math.toRadians(timeAngle.second)) * minuteHand).toFloat(),
            paint
        )
        paint.strokeWidth = 2.dp
        canvas.drawLine(
            width / 2f,
            height / 2f,
            width / 2f + (cos(Math.toRadians(timeAngle.third)) * secondHand).toFloat(),
            height / 2f + (sin(Math.toRadians(timeAngle.third)) * secondHand).toFloat(),
            paint
        )

        // 中心加个点
        paint.strokeWidth = 15.dp
        canvas.drawPoint(width / 2f, height / 2f, paint)

        postOnAnimationDelayed(runnable, 100)
    }

    private fun getTimeAngle(): Triple<Double, Double, Double> {
        // 获取系统时间
        val calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"))
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        val second = calendar.get(Calendar.SECOND)
        println("a = $hour, b = $minute, c = $second")
        val a = 360 * (hour / 12.0) + 270
        val b = 360 * (minute / 60.0) + 270
        val c = 360 * (second / 60.0) + 270
        return Triple(a, b, c)
    }
}